package org.jivesoftware.openfire.chatlogs;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;
import org.jivesoftware.admin.AuthCheckFilter;
import org.jivesoftware.util.ParamUtils;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ChatLogsServlet extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9067379257342484480L;
	private static final Logger log = LoggerFactory.getLogger(ChatLogsServlet.class);
	private static final ObjectMapper mapper = new ObjectMapper();
	 private static DbChatLogsManager logsManager;
	 
	 public void init(ServletConfig config) throws ServletException {
	        super.init(config);
	        AuthCheckFilter.addExclude("chatlogs/getchatlogs");
	        logsManager = DbChatLogsManager.getInstance();
	     
	        //avoid authority check
	       
	    }
	  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        doPost(request, response);
	    }
	  
	   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
       IOException {
		   response.setCharacterEncoding("utf-8");
	        PrintWriter out = response.getWriter();	
	        String action = ParamUtils.getParameter(request, "action");//first parameter
//	        ChatLogs logs = new ChatLogs();
	        if("querylogs".equals(action)){
	        	 String sender = ParamUtils.getParameter(request, "sender");//second parameter
	        	 String receiver = ParamUtils.getParameter(request, "receiver");//third parameter
	        	 String msgUser = ParamUtils.getParameter(request, "msgUser");
	        	
	        	 List<HashMap<String, Object>> list = logsManager.query(sender,receiver,msgUser);
	        	 log.info(list.toString());
	        	  StringWriter writer = new StringWriter();
	              mapper.writeValue(writer, list);
	              replyMessage(writer.toString(), response, out);
	        }
	        else if("deletelogs".equals(action)){
	        	String msgUser =  ParamUtils.getParameter(request, "msgUser");
	        	boolean deleted = logsManager.deleteWhoHistory(msgUser);
	        	log.info("deleted "+deleted);
	        	 StringWriter writer = new StringWriter();
	              mapper.writeValue(writer, deleted);
	              replyMessage(writer.toString(), response, out);
	        }
	   }
	   @Override
	    public void destroy() {
	        super.destroy();
	        // Release the excluded URL
	        AuthCheckFilter.removeExclude("chatlogs/getchatlogs");
	   
	    }
	private void replyMessage(String message, HttpServletResponse response,
			PrintWriter out) {
		// TODO Auto-generated method stub
		  response.setContentType("text/json");
		  log.info(message);
	        out.println(message);
	        out.flush();
	}
	 
}
